const express = require('express');
const cors = require('cors')
const bodyParser = require('body-parser')
const db = require('./db/db.js')
const jwtConfig = require('./jwt_config/index.js')
const {expressjwt: jwt} = require('express-jwt')
const Joi = require('joi');

const loginRouter = require('./router/login.js')
const announceRouter = require('./router/announce.js')
const carouselRouter = require('./router/carousel.js')

db(()=> {
  const port = '3007';
  const app = express();
  
  app.use(cors());
  app.use(bodyParser.urlencoded({ extended: false }))
  app.use(bodyParser.json())
  // app.use(jwt({ secret: jwtConfig.jwtSecretKey, algorithms: ['HS256'] })
  //    .unless({ path: [/^\/api\//] }));
     
  app.use((err, req, res, next)=> {
    if(err instanceof Joi.ValidationError) return res.cc(err);
  })
  
  // 登录路由
  app.use('/api', loginRouter)
  // 公告路由
  app.use('/announce', announceRouter)
  // 轮播图路由
  app.use('/carousel', carouselRouter)
  app.use('/uploads', express.static(__dirname + '/static/images/carousel'))
  
  app.listen(port, () => console.log('http://127.0.0.1:', port))
}, ()=> {
  console.error("数据库连接失败");
})